<script type="text/javascript">
$(function(){
/*省市联动*/
	$("[name='province_id']").change(function(){
		var id = $(this).val();
		$.ajax({
			type:"POST",
			dataType:'json',
			data:{'region':id,'type':1,'bool':1},
			url:"__APP__/Communal/get_province_city",
			success:function(data){
				var option = "";
				if(data){
					option += "<option value='-10'>请选择</option>";
					if(data){
						for(var i=0;i<data.length;i++){
							option += "<option value="+data[i].id+">"+data[i].name+"</option>";
						}
					}
				}else{
					option += "<option value='-10'>市</option>";
				}
				$("[name='city_id']").html(option).select2();
			}
		});
	});
	//添加联系人
	$(".add_cus").click(function(){
		var pid = "{$project['id']}",
			center_id = "{$project['center_id']}",
			arr = [];
		var refresh = $(this).attr("refresh");
		$("[name='refresh']").val(refresh);
		$("#customeEdit2").find(".ajax_error_tip2").text("").hide();
		$("[name='hide_val[]']").each(function(){
			arr.push($(this).val());
		});
		$('#customeModal').modal();
	});
	
	//编辑点击获取值
	$(".btn-edit").on("click",function(){
		var mid = $(this).attr("mid"),
		modal = $("#customeModal");
		modal.find(".modal-header h4").text("添加客户");
		modal.find(".btn-card").attr({"mid":mid});
		$.ajax({
			type:"POST",
			async:false,
			dataType:"json",
			data:{"id":mid},
			url:"__APP__/Crm/getInfoByContactId",
			success:function(data){
				modal.find(".username").val(data.username);
				modal.find(".company_name").val(data.company_name);
				modal.find(".job").val(data.job);
				modal.find(".job_level").val(data.job_level).select2();
				modal.find(".decision_relation").val(data.decision_relation).select2();
				modal.find(".province_id").val(data.province_id).select2();
				$("#city_id").html(data.city_info).select2();
				modal.find(".mobilephone").val(data.mobilephone);
				modal.find(".address").val(data.address);
				modal.find(".telephone").val(data.telephone);
				modal.find(".qq").val(data.qq);
				modal.find(".wx").val(data.wx);
				modal.find(".email").val(data.email);
				modal.find(".birthday").val(data.birthday);
				modal.find(".hobby").val(data.hobby);
				var gender = data.gender;
				if(gender==1){
					modal.find('.radio-inline input[name="gender"]').eq(0).click();
					modal.find('.radio-inline input[name="gender"]').eq(1).is(':checked',false);
				}else{
					modal.find('.radio-inline input[name="gender"]').eq(0).is(':checked',false);
					modal.find('.radio-inline input[name="gender"]').eq(1).click();
				}
				modal.find(".remark").val(data.remark);
				modal.modal();
			}
		});
	});
	
	
	//表单验证
	$(".btn-card").click(function(){
	
		var tag = true,		
			mid = $(this).attr("mid"),
			action;
		if(!mid){
			mid = "";
			$("[name='action']").val('add');
		}else{
			$("[name='action']").val('edit');
			$("[name='contact_id']").val(mid);
		}
		
		$(".required-text").each(function(){
			var $val = $(this).val();
			if($val=="" || /^\s+$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		$("select.required-select").each(function(){
			var $val = $(this).val();
			if($val==-10){
				$(this).nextAll(".error-item").show();
				tag = false;
			}
		});
		$("[name='mobilephone']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^1[3|4|5|7|8]\d{9}$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		$("[name='telephone']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^(\d{3,4}\-?)?\d{7,8}(\-\d{3,4})?$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		$("[name='email']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		$("[name='qq']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^[1-9]\d{4,10}$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		if($("[name='mobilephone']").val()=="" && $("[name='telephone']").val()==""){
			$("#customeModal").find(".ajax_error_tip2").text("手机号或座机号必须填写一项").show();
			tag = false;
		}
		if($("[name='gender']:checked").length==0){
			$("[name='gender']").parent("label").nextAll(".error-item").show();
			tag = false;
		}
		var refresh = $("[name='refresh']").val();
		if(tag){
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:$(".add-custome-info2").serialize(),
				url:"__APP__/Crm/contact_doadd",
				success:function(data){
					if(data.status==0){
						$("#customeModal").find(".ajax_error_tip2").text(data.msg).show();
					}else{
						$("#customeModal").modal('hide');
						$('.add_cus').next(".error-item").hide();
						if(refresh==1){
							window.location.reload();
						}else{
							crm_contact();
						}
					}
				}
			});
		}
	});
	
	//取消
	$("#customeModal").on('hidden.bs.modal',function(){
		$(this).find(".error-item").hide();
		$(this).find("select").val(-10).select2();
		$(this).find(".btn-affirm").removeAttr("mid");
		var refresh = $("[name='refresh']").val();
		if(refresh==1){
			window.location.reload();
		}
	});
});

function crm_contact() {
    $.ajax({
        type: "GET",
        async: false,
        dataType: "json",
        url: "__APP__/Crm/getCrmContact",
        success: function (data) {
            var html = "<option value='-10'>请选择</option>";
            var html2 = "";
            for (var i = 0; i < data.length; i++) {
                html += "<option selected value=" + data[i].contact_id + ">" + data[i].username + "-" + data[i].mobilephone + "</option>"
                html2 += "<option value=" + data[i].contact_id + ">" + data[i].username + "-" + data[i].mobilephone + "</option>"
            }
            $("[name='main_linkman']").html(html).select2();
            $("[id='other_linkman']").html(html2).select2();
        }
    })
}
</script>